package com.cch.security;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.util.StringUtils;

import java.io.IOException;

/**
 * @author cch
 * @date 2025年05月21日 15:14
 */
public class PhoneNumAuthenticationFilter extends AbstractAuthenticationProcessingFilter {

    /**
     * form表单中手机号码的字段name
     */
    public static final String SPRING_SECURITY_FORM_MOBILE_KEY = "mobile";
    // 前端手机验证码参数
    public static final String SPRING_SECURITY_FORM_CODE_KEY = "code";
    private String mobileParameter = SPRING_SECURITY_FORM_MOBILE_KEY;
    private String codeParameter = SPRING_SECURITY_FORM_CODE_KEY;
    private static final AntPathRequestMatcher DEFAULT_ANT_PATH_REQUEST_MATCHER = new AntPathRequestMatcher("/mobile", "POST");
    /**
     * 是否仅 POST 方式
     */
    private boolean postOnly = true;

    public PhoneNumAuthenticationFilter() {
        // 短信登录的请求 post 方式的 /phone
        super(DEFAULT_ANT_PATH_REQUEST_MATCHER);
    }
    public PhoneNumAuthenticationFilter(AuthenticationManager authenticationManager) {
        super(DEFAULT_ANT_PATH_REQUEST_MATCHER, authenticationManager);
    }
    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
        if (postOnly && !request.getMethod().equals("POST")) {
            throw new AuthenticationServiceException(
                    "Authentication method not supported: " + request.getMethod());
        }

        // 电话号码
        String mobile = obtainUsername(request);
        System.out.println("电话：" + mobile);
        if (StringUtils.isEmpty(mobile)) {
            throw new AuthenticationServiceException("电话号码不能为空");
        }
        String code = getCode(request);
        System.out.println("验证码：" + code);
        // 组装AuthenticationToken
        PhoneNumAuthenticationToken phoneNumAuthenticationToken = new PhoneNumAuthenticationToken(mobile);
        setDetails(request, phoneNumAuthenticationToken);
        return this.getAuthenticationManager().authenticate(phoneNumAuthenticationToken);
    }

    protected void setDetails(HttpServletRequest request, PhoneNumAuthenticationToken authRequest) {
        authRequest.setDetails(this.authenticationDetailsSource.buildDetails(request));
    }

    protected String obtainUsername(HttpServletRequest request) {
        return request.getParameter(mobileParameter);
    }

    /**
     * 获取前端的验证码
     */
    public String getCode(HttpServletRequest request ){
        return request.getParameter(codeParameter);
    }

    public void setPostOnly(boolean postOnly) {
        this.postOnly = postOnly;
    }


}
